<?php
// +----------------------------------------------------------------------
// | zhanshop-cloud / Disk.php    [ 2025/3/5 10:40 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2025 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace extend\devops;

use zhanshop\App;

class Disk
{
    /**
     * 硬盘检查
     * @param float $valve
     * @param mixed|null $callback
     * @return void
     */
    public function inspect(float $valve = 0.8, mixed $callback = null)
    {
        $directoryPath = "/";
        $diskFreeSpace = disk_free_space($directoryPath);
        $diskTotalSpace = disk_total_space($directoryPath);
        $diskUsedSpace = $diskTotalSpace - $diskFreeSpace;
        $diskUsableRate = round($diskUsedSpace / $diskTotalSpace, 2);
        if($diskUsableRate >= $valve && $callback != null){
            // 触发预警
            $callback($diskUsableRate);
        }
        $data = [
            'total' => $diskTotalSpace,
            'used' => $diskUsedSpace,
            'used_rate' => $diskUsableRate,
        ];
        error_log(date('H:i:s')."###".json_encode($data)."\n", 3, App::runtimePath().'/devops/disk/'.date("Ymd").'.log');
    }
}